package com.foreknow.controller;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class LifeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置服务器端向客户端响应的内容的类型(MIME)
        resp.setContentType("text/html;charset=utf-8");
        //设置客户端向服务器端请求的字符编码集为utf-8
        req.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.println("Servlet的生命周期");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req,resp);
    }

    @Override
    public void destroy() {
        System.out.println("destroy......");
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
        //如何获取初始化的参数getInitParameter()
        //String pageSize = config.getInitParameter("pageSize");
        //int p = Integer.parseInt(pageSize);
        //获取到全局初始化参数
        String encoding = config.getServletContext().getInitParameter("encoding");
        System.out.println(encoding);
    }

//    @Override
//    public void init() throws ServletException {
//        //只能调用一次
//        System.out.println("初始化的init方法......");
//    }
}